package com.controller;

import com.bean.BookLike;
import com.common.Result;
import com.service.BookLikeService;
import com.utils.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/book-likes")
public class BookLikeController {
    @Autowired
    private BookLikeService bookLikeService;

    /**
     * 根据点赞 id 获取书籍点赞信息
     * @param likeId 点赞 id
     * @return 封装了书籍点赞信息的通用结果对象，包含状态码、消息和数据
     */
    @GetMapping("/{likeId}")
    public Result<BookLike> getBookLikeById(@PathVariable Integer likeId) {
        BookLike bookLike = bookLikeService.getBookLikeById(likeId);
        if (bookLike != null) {
            return Result.success(bookLike);
        }
        return Result.fail("书籍点赞信息不存在");
    }

    /**
     * 分页模糊查询书籍点赞信息，可根据书籍标题、用户姓名等进行模糊匹配
     * @param keyword 模糊查询关键字，可匹配书籍标题、用户姓名等
     * @param pageNum 当前页码
     * @param pageSize 每页记录数
     * @return 封装了分页书籍点赞信息的通用结果对象，包含状态码、消息和数据
     */
    @GetMapping
    public Result<PageInfo<BookLike>> getBookLikesWithKeyword(
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo<BookLike> pageInfo = bookLikeService.getBookLikesWithKeyword(keyword, pageNum, pageSize);
        return Result.success(pageInfo);
    }

    /**
     * 插入新的书籍点赞信息
     * @param bookLike 书籍点赞信息对象
     * @return 封装了插入结果的通用结果对象，包含状态码、消息和插入是否成功的标志
     */
    @PostMapping
    public Result<Boolean> insertBookLike(@RequestBody BookLike bookLike) {
        boolean result = bookLikeService.insertBookLike(bookLike);
        if (result) {
            return Result.success(true);
        }
        return Result.fail("插入书籍点赞信息失败");
    }

    /**
     * 根据点赞 ID 删除书籍点赞信息
     * @param likeId 点赞 ID
     * @return 封装了删除结果的通用结果对象，包含状态码、消息和删除是否成功的标志
     */
    @DeleteMapping("/{likeId}")
    public Result<Boolean> deleteBookLikeById(@PathVariable Integer likeId) {
        boolean result = bookLikeService.deleteBookLikeById(likeId);
        if (result) {
            return Result.success(true);
        }
        return Result.fail("删除书籍点赞信息失败，点赞记录可能不存在");
    }
}